import time

import jwt
from flask import current_app


class JwtTool:
    def create(self,payload,ex=3600):
        payload['exp'] = int(time.time()) * ex
        secret_key = current_app.config.get('SECRET_KEY')
        token = jwt.encode(payload,secret_key,algorithm='HS256')
        return token

    def vaild(self,token):
        secret_key = current_app.config.get('SECRET_KEY')
        try:
            payload = jwt.decode(token,secret_key,algorithms='HS256')
            return payload
        except Exception as e:
            print(e)